package com.roberto.entities;

import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.Rectangle;
import com.roberto.main.Main;

public class Shield {
    private Sprite[] sprite = new Sprite[10];
    private final float x, y;
    private int hp;
    private int step;
    private float xx, yy;

    public Shield(float gap) {
        x = Main.WIDTH / 9.7f + (gap * (Main.WIDTH / 4.12f));
        y = 120;
        createShield();

    }

    private void createShield() {
        int size = 20;
        int texX = 127;
        sprite[0] = new Sprite(Main.megaTexture, texX, 2, size, size);
        sprite[0].setPosition(x, y);
        sprite[1] = new Sprite(Main.megaTexture, texX, 2, size, size);
        sprite[1].setPosition(x, y + size);
        sprite[2] = new Sprite(Main.megaTexture, texX, 42, size, size);
        sprite[2].setPosition(x + size, y + size);
        sprite[3] = new Sprite(Main.megaTexture, texX, 24, size, size - 3);
        sprite[3].setPosition(x, y + size * 2);
        sprite[4] = new Sprite(Main.megaTexture, texX, 42, size, size);
        sprite[4].setPosition(x + size * 2, y + size);
        sprite[4].flip(true, false);
        sprite[5] = new Sprite(Main.megaTexture, texX, 2, size, size);
        sprite[5].setPosition(x + size, y + size * 2 - 3);
        sprite[6] = new Sprite(Main.megaTexture, texX, 2, size, size);
        sprite[6].setPosition(x + size * 2, y + size * 2 - 3);
        sprite[7] = new Sprite(Main.megaTexture, texX, 2, size, size);
        sprite[7].setPosition(x + size * 3, y);
        sprite[8] = new Sprite(Main.megaTexture, texX, 2, size, size);
        sprite[8].setPosition(x + size * 3, y + size);
        sprite[9] = new Sprite(Main.megaTexture, texX, 24, size, size - 3);
        sprite[9].flip(true, false);
        sprite[9].setPosition(x + size * 3, y + size * 2);
    }

    public Sprite getSprite(int i) {
        return sprite[i];
    }

    public final Rectangle getBounds(final int i) {
        return sprite[i].getBoundingRectangle();
    }

    public void damageZone(int i) {
        hp = sprite[i].getRegionX();
        xx = sprite[i].getX();
        yy = sprite[i].getY();

        hp = hp <= 127 ? 127 : hp;
        step = hp == 149 ? 21 : 22;

        if (i == 9 || i == 4) {
            hp -= 20;
        }
        if (hp < 192) {
            sprite[i] = new Sprite(Main.megaTexture, hp + step, sprite[i].getRegionY(), (int) sprite[i].getWidth(),
                    (int) sprite[i].getHeight());
            sprite[i].setPosition(xx, yy);
            if (i == 9 || i == 4) {
                sprite[i].flip(true, false);
            }
        } else if (i == 9 && i == 4) {
            sprite[i] = new Sprite(Main.megaTexture, 187, 40, 20, (int) sprite[i].getHeight());
            sprite[i].flip(true, false);
            sprite[i].setPosition(xx, yy);
        } else {
            sprite[i].setPosition(-100, -100);
        }

    }
}
